package me

/**
  * Created by lzd on 2019/1/25.
  */
object map {
  def main(args: Array[String]): Unit = {
    map
  }

  def map: Unit = {
    //不可变
    val map1 = Map("Alice" -> 10, "Bob" -> 20, "Kotlin" -> 30)
    //可变
    val map2 = scala.collection.mutable.Map("Alice" -> 10, "Bob" -> 20, "Kotlin" -> 30)

    //空的映射( 必须是可变的，不可变的空映射没有意义)
    val map3 = new scala.collection.mutable.HashMap[String, Int]

    //get返回类型是Option
    map2.get("Bob") match {
      case Some(value) => print(value)
      case None => println("毛都不剩")
    }

    //替换值
    map2("Alice") = 99
    println(map2("Alice"))
    // 加值，存在就覆盖
    map2 += ("Bob" -> 99)
    //减值
    map2 -= ("Alice", "Kotlin")
    println(map2)
    // 加值
    val map5 = map2 + ("AAA" -> 10, "BBB" -> 20)
    println(map5)

    //遍历
    for ((k, v) <- map1) println(k + " is mapped to " + v)
    for (v <- map1.keys) println(v)
    for (v <- map1.values) println(v)
    for (v <- map1) println(v)

  }
}
